//
//  BaseViewController.m
//  iHappySDK
//
//  Created by telen on 14-8-19.
//  Copyright (c) 2014年 刘赞黄Telen. All rights reserved.
//

#import "IHBaseViewController.h"
#import "ConstDefine.h"

@interface IHBaseViewController ()

@end

@implementation IHBaseViewController

- (void)dealloc
{
    self.imgView = nil;
    self.textField_1 = nil;
    self.textField_2 = nil;
    self.textField_3 = nil;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

//MARK: Tap隐藏键盘事件
- (void)didTapView:(NSObject*)sender
{
    [self.textField_1 resignFirstResponder];
    [self.textField_2 resignFirstResponder];
    [self.textField_3 resignFirstResponder];
}

//MARK: 键盘事件
- (void)keyboardWillShow:(NSNotification*)note
{
    lastNote = note;
    //
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
    
    UITextField* textField = nil;
    if ([_textField_1 isFirstResponder]) {
        textField = _textField_1;
    }else if ([_textField_2 isFirstResponder]) {
        textField = _textField_2;
    }else if ([_textField_3 isFirstResponder]) {
        textField = _textField_3;
    }
    
    CGFloat pod = 18/ratio_w;
    CGFloat h_all = self.view.frame.size.height;
    // get a rect for the textView frame
    CGRect textFrame = textField.frame;
    textFrame.origin = [self.view convertPoint:textFrame.origin fromView:self.imgView];
    
    //
    CGPoint pt = textFrame.origin;
    if (pt.y > pod
        && (pt.y+textFrame.size.height) < (h_all - keyboardBounds.size.height - pod)
        ) {
        return;	
    }
    
    CGFloat distance = (h_all-keyboardBounds.size.height-pod-textFrame.size.height) - pt.y;
    
    CGRect bounds = self.imgView.bounds;
    [self change_imgView_constraint_AttributeCenterY:distance];
    [UIView animateWithDuration:duration.doubleValue animations:^{
        self.imgView.bounds = bounds;
        [self.imgView layoutIfNeeded];
    }];
    
    
}

- (void)keyboardWillHide:(NSNotification*)note
{
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    lastNote = nil;
    
    CGRect bounds = self.imgView.bounds;
    [self change_imgView_constraint_AttributeCenterY:0];
    [UIView animateWithDuration:duration.doubleValue animations:^{
        self.imgView.bounds = bounds;
        [self.imgView layoutIfNeeded];
    }];
}

- (void)keyboardWillChangeFrame:(NSNotification*)note
{
    CGRect keyboardFrame;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    keyboardFrame = [self.view convertRect:keyboardFrame toView:nil];
    
    CGPoint pt = keyboardFrame.origin;
    pt.y -= 1;pt.x = 2;
    
    //
    CGRect lastFrame;
    [[lastNote.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&lastFrame];
    CGFloat h1 = lastFrame.size.height;
    CGFloat h2 = keyboardFrame.size.height;
    
    if (CGRectContainsPoint(self.view.frame,pt)) {
        if(!lastNote || fabsf(h1 - h2) > 0.1)[self keyboardWillShow:note];
        lastNote = note;
    }else{
        [self keyboardWillHide:note];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(lastNote)[self keyboardWillShow:lastNote];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(lastNote)[self keyboardWillHide:lastNote];
}


//MARK: 界面初始化
- (void)viewDidLoad
{
    [super viewDidLoad];
    //tap
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapView:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:tap];
    
    //keyBoardEvent
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}

- (void)change_imgView_constraint_AttributeCenterY:(CGFloat)constant
{
    NSArray* arr = self.imgView.superview.constraints;
    for (NSLayoutConstraint* constraint in arr) {
        if (constraint.firstItem == self.imgView
            && constraint.firstAttribute == NSLayoutAttributeCenterY) {
            constraint.constant = constant;
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self didTapView:nil];
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)autoLayout_to_centerForView:(UIView *)view
{
    [view lyt_centerInParent];
}

- (void)showMessage:(NSString*)str
{
    [[WaitingShow shareSingleShow] showString:str andAutoHide:YES];
}


@end
